package com.ruoyi.web.controller.biz;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BsQiye;
import com.ruoyi.system.domain.excel.BsComplainExcel;
import com.ruoyi.system.domain.excel.BsQiyeExcel;
import com.ruoyi.system.domain.ro.BsQiyeRo;
import com.ruoyi.system.service.IBsQiyeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 涉旅企业管理
 *
 * @author ruoyi
 * @date 2023-10-29
 */
@Validated
@RequiredArgsConstructor
@Api(tags = "WEB端-涉旅企业")
@RestController
@RequestMapping("/bs/qiye")
public class BsQiyeController extends BaseController {

    private final IBsQiyeService iBsQiyeService;

    @ApiOperation(value = "店铺-列表导出", tags = {"WEB端-涉旅企业", "管理端小程序-店铺"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "商店类型 详见字典merchant_type"),
            @ApiImplicitParam(name = "jingquId", value = "景区id"),
            @ApiImplicitParam(name = "applyName", value = "申请人"),
            @ApiImplicitParam(name = "name", value = "商铺名称"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典merchant_type"),
            @ApiImplicitParam(name = "beginCreateTime", value = "申请时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "申请时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/export")
    public AjaxResult<?> export(@RequestParam(required = false) String type,
                                   @RequestParam(required = false) Long jingquId, @RequestParam(required = false) String applyName,
                                   @RequestParam(required = false) String name, @Pattern(regexp = "^(0)$|^(1)$|^(2)$|^$", message = "auditStatus不合法") @RequestParam(required = false) String auditStatus,
                                   @RequestParam(required = false) String beginCreateTime, @RequestParam(required = false) String endCreateTime) {
        BsQiyeRo ro = BsQiyeRo.builder().type(type).jingquId(jingquId).applyName(applyName).name(name).auditStatus(auditStatus).build();
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsQiye> list = iBsQiyeService.queryList(ro, false);
        ExcelUtil<BsQiyeExcel> util = new ExcelUtil<>(BsQiyeExcel.class);
        return util.exportExcel(ConvertUtil.sourceToTarget(list, BsQiyeExcel.class), "涉旅企业");
    }

    @ApiOperation(value = "店铺-列表", tags = {"WEB端-涉旅企业", "管理端小程序-店铺"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "商店类型 详见字典merchant_type"),
            @ApiImplicitParam(name = "jingquId", value = "景区id"),
            @ApiImplicitParam(name = "applyName", value = "申请人"),
            @ApiImplicitParam(name = "name", value = "商铺名称"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典merchant_type"),
            @ApiImplicitParam(name = "beginCreateTime", value = "申请时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "申请时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsQiye>> enterList(@RequestParam(required = false) String type,
                                                   @RequestParam(required = false) Long jingquId, @RequestParam(required = false) String applyName,
                                                   @RequestParam(required = false) String name, @Pattern(regexp = "^(0)$|^(1)$|^(2)$|^$", message = "auditStatus不合法") @RequestParam(required = false) String auditStatus,
                                                   @RequestParam(required = false) String beginCreateTime, @RequestParam(required = false) String endCreateTime) {
        startPage();
        BsQiyeRo ro = BsQiyeRo.builder().type(type).jingquId(jingquId).applyName(applyName).name(name).auditStatus(auditStatus).build();
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsQiye> list = iBsQiyeService.queryList(ro, false);
        return Result.success(getDataTable(list));
    }

    @ApiOperation(value = "店铺-详情", tags = {"WEB端-涉旅企业", "管理端小程序-店铺", "商户端-店铺入驻&我的店铺", "WEB端-文创数据"})
    @ApiImplicitParam(name = "id", value = "企业id")
    @GetMapping("/detail")
    public Result<BsQiye> getInfo(@RequestParam Long id) {
        return Result.success(iBsQiyeService.queryById(id));
    }

    @ApiOperation(value = "审核", tags = {"WEB端-涉旅企业", "管理端小程序-店铺"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditStatus", value = "审批状态 0待审核 1已通过 2已驳回 详见字典audit_status"),
            @ApiImplicitParam(name = "id", value = "企业id"),
            @ApiImplicitParam(name = "auditReason", value = "拒绝原因（非必填）"),
    })
    @Log(title = "WEB端-涉旅企业-店铺审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/audit")
    public Result<?> audit(@RequestParam Long id, @Pattern(regexp = "^(1)$|^(2)$", message = "auditStatus不合法") @RequestParam String auditStatus, @RequestParam(required = false) String auditReason) {
        BsQiyeRo ro = BsQiyeRo.builder()
                .id(id)
                .auditStatus(auditStatus)
                .auditBy(SecurityUtils.getLoginUser().getUser().getNickName())
                .auditTime(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN))
                .auditReason(auditReason)
                .build();
        return Result.toResult(iBsQiyeService.auditByBo(ro));
    }


    @ApiOperation(value = "修改店铺状态", tags = {"WEB端-涉旅企业", "WEB端-文创数据"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "营业状态 0：正常营业 1：暂停营业 2:店铺撤场 详见字典business_status"),
            @ApiImplicitParam(name = "id", value = "企业id"),
    })
    @Log(title = "WEB端-涉旅企业-修改店铺状态", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/edit")
    public Result<?> edit(@RequestParam Long id, @Pattern(regexp = "^(0)$|^(1)$|^(2)$", message = "status不合法") @RequestParam String status) {
        BsQiyeRo ro = BsQiyeRo.builder().id(id).status(status).build();
        return Result.toResult(iBsQiyeService.updateStatus(ro));
    }


}
